package com.platform.core.sms;

//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONException;
//import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;

import java.util.*;
import java.util.Map.Entry;

@Log4j2
/**
 * 建行短信辅助类
 */
public class AlgorithmOriSplicingUtil {

    public static void main(String[] args) {
        String json = "{'Name':'BeJson','SIGN_INF':'asd12asd1231sd123','Url':'http://www.bejson.com','Page':88,'IsNonProfit':true,'Address':'科技园路','FEEGRP':[{'SN':'2','Url':'http://www.google.com'},{'SN':'1','Url':'http://www.baidu.com'},{'SN':'0','Url':'http://www.SoSo.com'}],'TAXGRP':[{'SN':'5','Aaa':'111'},{'SN':'4','Bbb':'222'},{'SN':'3','Ccc':'333'}]}";
        Set<String> set = new HashSet<>();
        set.add("FEEGRP");
        set.add("TAXGRP");
        set.add("LIST");
//        createSign(json, set);
    }

    /**
     * 拼接原串
     */
    @SuppressWarnings("rawtypes")
    private static String splicingSign(SortedMap<String, Object> parameters, Set<String> set) {
        StringBuilder buffer = new StringBuilder();
        Set es = parameters.entrySet();
        for (Object e : es) {
            Entry entry = (Entry) e;
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (set.contains(k)) {
                buffer.append(v);
            } else {
                if (!"SIGN_INF".equals(k) && !"Svc_Rsp_St".equals(k)) {
                    buffer.append(k).append("=").append(v).append("&");
                }
            }
        }
        return buffer.toString();
    }

    /**
     * 生成原串
     */
  /*  public static String createSign(String json, Set<String> set) {
        SortedMap<String, Object> sortedMap = new TreeMap<>();
        //转换JSON对象
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //遍历循坏外字段
        assert jsonObject != null;
        for (Entry<String, Object> entry : jsonObject.entrySet()) {
            if (null != entry.getValue()
                    && !"".equals(entry.getValue())
                    && !set.contains(entry.getKey())) {
                sortedMap.put(entry.getKey(), entry.getValue());
            }
        }
        if (set != null && !set.isEmpty()) {
            //处理集合字段
            for (String listKey : set) {
                String listValue = processingSet(jsonObject, listKey);
                sortedMap.put(listKey, listValue);
            }
        }
        //字段拼接
        String sign = splicingSign(sortedMap, set);
        sign = sign.substring(0, sign.length() - 1);
        log.info("生成原串:" + sign);
        return sign;
    }
*/
    /**
     * 处理集合内字段
     */
    /*private static String processingSet(JSONObject jsonObj, String listKey) {
        SortedMap<String, Object> sortedMap;
        Set<String> set = new HashSet<>();
        //返回拼接的原串
        StringBuilder buffer = new StringBuilder();
        //根据key获取集合json字符串
        String list = jsonObj.getString(listKey);
        if (list != null && !"".equals(list)) {
            //list转换对象
            JSONArray jarr = JSONArray.parseArray(list);
            //创建json集合
            List<JSONObject> jsonValues = new ArrayList<>();
            for (int i = 0; i < jarr.size(); i++) {
                jsonValues.add(jarr.getJSONObject(i));
            }
            //将json集合按照SN排序
            jsonValues.sort((a, b) -> {
                Integer valA = a.getInteger("SN");
                Integer valB = b.getInteger("SN");
                return valA.compareTo(valB);
            });
            //暂存每一个子元素拼接的值
            String s;
            //遍历排序后的集合
            for (JSONObject j : jsonValues) {
                sortedMap = new TreeMap<>();
                for (Entry<String, Object> entry : j.entrySet()) {
                    if (null != entry.getValue() && !"".equals(entry.getValue())) {
                        sortedMap.put(entry.getKey(), entry.getValue());
                    }
                }
                s = splicingSign(sortedMap, set);
                buffer.append(s);
            }
            return buffer.toString();
        }
        return buffer.toString();
    }*/
}
